/*
	Copyright (C) 2010- Peer internet solutions
	
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Modified by: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.mix.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.grupoavatar.guiaubicua.VInicial;
import org.grupoavatar.guiaubicua.entidades.ARMarker;
import org.grupoavatar.guiaubicua.mix.Marker;
import org.grupoavatar.guiaubicua.mix.MixContext;

import android.location.Location;
import android.util.Log;


/**
 * DataHandler is the model which provides the Marker Objects.
 * 
 * DataHandler is also the Factory for new Marker objects.
 */
public class DataHandler {

	private List<Marker> markerList = new ArrayList<Marker>();
	private List<Marker> markerListAlter = null;
	private List<Marker> markerListTotal = null;
	private Encuesta encuesta = null;

	public List<Marker> getMarkerListTotal() {
		return markerListTotal;
	}

	public void setMarkerListTotal(List<Marker> markerListTotal) {
		this.markerListTotal = markerListTotal;
	}

	public void addMarkers(List<Marker> markers) {
//		if (markerList != null) {
//			Log.v(VInicial.TAG, "Marker before: " + markerList.size());
//			for (Marker ma : markers) {
//				if((!markerList.contains(ma)) && (!contieneCodigo(ma)))
//						markerList.add(ma);
//			}
//			markerListAlter = markerList;
//			Log.d(VInicial.TAG, "Marker count: " + markerList.size());
//		}
		if (markerList != null) 
			Log.v(VInicial.TAG, "Marker before: " + markerList.size());
			
		markerListAlter = markerList = markers;
		if (markerList != null)
			Log.d(VInicial.TAG, "Marker count: " + markerList.size());
		
	}

	public void sortMarkerList() {
		Collections.sort(markerList);
	}

	public void updateDistances(Location location) {
		for (Marker ma : markerList) {
			float[] dist = new float[3];
			Location.distanceBetween(ma.getLatitude(), ma.getLongitude(),
					location.getLatitude(), location.getLongitude(), dist);
			ma.setDistance(dist[0]);
		}
	}

	public void updateActivationStatus(MixContext mixContext) {
		if (markerList != null) {
			if (markerList.size() == 1)	{
				Marker ma = markerList.get(0);
				ma.setActive(true);
				ma.setUnicoMarker(true);
			}
			else	{
				for (Marker ma : markerList) {
					ma.setActive(true);
					ma.setUnicoMarker(false);
				}
			}
		}
	}

	public void onLocationChanged(Location location) {
		updateDistances(location);
		sortMarkerList();
		for (Marker ma : markerList) {
			ma.update(location);
		}
	}

	public List<Marker> getMarkerList() {
		return markerList;
	}
	
	public Encuesta getEncuesta(){
		return encuesta;
	}
	
	public void setEncuesta(Encuesta encuesta) {
		this.encuesta = encuesta;
	}

	public List<Marker> getMarkerListForSearch() {
		return markerListAlter;
	}
	
	public void setMarkerListForSearch(List<Marker> markerList) {
		this.markerListAlter = markerList;
	}
	
	public void setMarkerList(List<Marker> markerList) {
		this.markerList = markerList;
	}

	public void clearMarkers() {
		if (markerList != null)
			markerList.clear();
	}
	
	public int getMarkerCount() {
		if (markerList != null)
			return markerList.size();
		else
			return 0;
	}

	public Marker getMarker(int index) {
		if (markerList != null)
			return markerList.get(index);
		return
			null;
	}
	
	public void relacionarMarkers() {
		for (int i = 0; i < markerListTotal.size(); i++) {
			ARMarker ma = (ARMarker) markerListTotal.get(i); 

			if (ma.getPoiIdHijos() != null && ma.getPoiIdHijos().size() > 0) {
				if (ma.getHijos() == null) {
					ma.inicializaListaHijos();
				}
				ArrayList<ARMarker> arr = ma.getHijos();
				for (int j = 0; j < ma.getPoiIdHijos().size(); j++)
					arr.add(obtenerMarker(ma.getPoiIdHijos().get(j)));
			} else if (ma.getPoiIdPadre() != null && !ma.getPoiIdPadre().equalsIgnoreCase("")) {
				ma.setPoiPadre(obtenerMarker(ma.getPoiIdPadre()));
			}
		}
	}
	
	public ARMarker obtenerMarker(String id) {
		int i = 0;
		boolean encontrado = false;
		ARMarker poi = null;
		if (markerListTotal != null && !id.equalsIgnoreCase("")) {
			while(!encontrado && i < markerListTotal.size()) {
				if (markerListTotal.get(i).getID().equalsIgnoreCase(id)) {
					poi = (ARMarker) markerListTotal.get(i);
					encontrado = true;
				}
				i++;
			}
		}
		return poi;
	}
}
